package com.papper.mng.controller.base;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.papper.common.bean.base.BaseConstWorkdate;
import com.papper.common.constant.SystemConstants;
import com.papper.common.enums.DataStatusEnum;
import com.papper.common.exception.ServiceException;
import com.papper.common.service.DwzPage;
import com.papper.common.service.base.BaseConstWorkdateService;
import com.papper.common.utils.tool.DateTool;
import com.papper.common.utils.tool.StringTool;
import com.papper.mng.controller.BaseAction;
import com.papper.mng.domain.DwzResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * @Author:LaJiao
 * @Date:2022-03-29 10:06:49
 * @Description:工作日
 */
@Controller
@RequestMapping("/base/const/workdate")
public class BaseConstWorkdateController extends BaseAction {

    @Autowired
    BaseConstWorkdateService baseConstWorkdateService;

    /**
     * 工作日
     *
     * @return
     */
    @RequestMapping(value = "")
    public String execute(HttpServletRequest _request, Model model) {
        // 字段查询条件
        Map<String, Object> objects = Maps.newHashMap();
        extraParams(objects, _request, BaseConstWorkdate.class);
        modelParm(objects, model);
        // 分页查询条件
        int pageNum    = StringTool.parseInteger(_request.getParameter("pageNum"), 1);
        int numPerPage = StringTool.parseInteger(_request.getParameter("numPerPage"), SystemConstants.PAGE_LIMIT_16);
        // 数据查询组装
        Example example = buildExample(objects, model, BaseConstWorkdate.class);
        example.setOrderByClause("date_day desc");
        PageInfo<BaseConstWorkdate> pageRtn = baseConstWorkdateService.findPage(pageNum, numPerPage, example);
        model.addAttribute("page", new DwzPage(pageNum, numPerPage, pageRtn.getTotal()));
        model.addAttribute("records", pageRtn.getList());
        return render("base/const/workdate/index");
    }

    /**
     * 工作日_新增
     */
    @GetMapping(value = "/add")
    public String add_get() {
        return render("base/const/workdate/add");
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public DwzResponse add_post(HttpServletRequest _request, BaseConstWorkdate baseConstWorkdate) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (DateTool.inFormat(baseConstWorkdate.getDateDay(), "yyyy-MM-dd")) {
            insertDay(baseConstWorkdate.getDateDay());
        } else if ((DateTool.inFormat(baseConstWorkdate.getDateDay(), "yyyy-MM"))) {
            Date firstMonth = DateTool.getFirstOfMonth(DateTool.parseDate(baseConstWorkdate.getDateDay(), "yyyy-MM"));
            Date endMonth   = DateTool.getEndOfMonth(DateTool.parseDate(baseConstWorkdate.getDateDay(), "yyyy-MM"));
            Date temp       = firstMonth;
            while (!DateTool.isSameDay(temp, endMonth)) {
                insertDay(DateTool.formatDate(temp, "yyyy-MM-dd"));
                temp = DateTool.addDays(temp, 1);
            }
            insertDay(DateTool.formatDate(endMonth, "yyyy-MM-dd"));
        }
        return new DwzResponse("200", "工作日添加成功!!!", "closeCurrent");
    }

    private void insertDay(String dateDay) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(DateTool.parseDate(dateDay, "yyyy-MM-dd"));
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }

        BaseConstWorkdate constWorkdate = new BaseConstWorkdate();
        constWorkdate.setDateDay(dateDay);
        if (w >= 1 && w <= 5) {
            constWorkdate.setT1Status(1);
        } else {
            constWorkdate.setT1Status(0);
        }
        constWorkdate.setT0Status(1);
        constWorkdate.setDataStatus(DataStatusEnum.OPEN.getCode());
        constWorkdate.setCreateTime(new Date());
        constWorkdate.setModifyTime(new Date());
        try {
            baseConstWorkdateService.save(constWorkdate);
        } catch (Exception e) {
            if (!e.getCause().toString().contains("Duplicate entry")) {
                throw e;
            }
        }
    }

    /**
     * 工作日_更新
     */
    @GetMapping(value = "/edit")
    public String edti_get(Model model, Long id) {
        BaseConstWorkdate entity = baseConstWorkdateService.findById(id);
        model.addAttribute("entity", entity);
        return render("base/const/workdate/edit");
    }

    @PostMapping(value = "/edit")
    @ResponseBody
    public DwzResponse edit_post(HttpServletRequest _request, BaseConstWorkdate baseConstWorkdate) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (baseConstWorkdate == null || baseConstWorkdate.getId() == null) {
            return new DwzResponse("300", "参数异常！");
        }
        try {
            baseConstWorkdate.setModifyTime(new Date());
            baseConstWorkdateService.update(baseConstWorkdate);
            return new DwzResponse("200", "工作日修改成功!!!", "closeCurrent");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "工作日修改失败:" + e.getMessage());
        }
    }

    /**
     * 工作日_删除
     *
     * @param id
     * @
     */
    @PostMapping(value = "/del")
    @ResponseBody
    public DwzResponse del_post(HttpServletRequest _request, Long id) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (id == null || id.intValue() <= 0) {
            return new DwzResponse("300", "参数ID为空！");
        }

        BaseConstWorkdate old = baseConstWorkdateService.findById(id);

        if (old == null) {
            return new DwzResponse("300", "参数[id]信息异常！");
        }
        try {
            baseConstWorkdateService.deleteById(id);
            return new DwzResponse("200", "工作日删除成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "工作日删除失败:" + e.getMessage());
        }
    }

    /**
     * 工作日_详情
     *
     * @return
     */
    @GetMapping(value = "/detail")
    public String detail(HttpServletResponse _response, Model model, Long id) {
        BaseConstWorkdate baseConstWorkdate = baseConstWorkdateService.findById(id);
        model.addAttribute("entity", baseConstWorkdate);
        return render("base/const/workdate/detail");
    }

}



